package tech.eyao.hs60x.task;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import tech.eyao.hs60x.co.ShakeCo;
import tech.eyao.hs60x.dto.ShakeDTO;
import tech.eyao.hs60x.utils.LocationUtils;

/**
 * @author hejian
 * @date 2018年1月24日 上午11:45:00
 */
public class ShakeTask extends AbstractTask<ShakeDTO> {
	// 经度
	private double longitude;
	// 纬度
	private double latitude;
	private Set<ShakeCo> set;
	private long accountId;
	
	public ShakeTask(Long accountId,Set<ShakeCo> set,double longitude, double latitude) {
		this.longitude = longitude;
		this.latitude = latitude;
		this.set = set;
		this.accountId = accountId;
	}

	@Override
	public List<ShakeDTO> businessProcess() {
		List<ShakeDTO> list = set.stream().filter(s->s.watchId != accountId)
				.map(s->new ShakeDTO(s.watchId, s.nick, LocationUtils.getDistanceM(latitude, longitude, s.latitude, s.longitude))).
				collect(Collectors.toList());
		// sort
		Collections.sort(list, new Comparator<ShakeDTO>(){
			@Override
			public int compare(ShakeDTO o1, ShakeDTO o2) {
				if(o1.distance > o2.distance){  
                    return 1;  
                }  
                if(o1.distance == o2.distance){  
                    return 0;  
                }  
                return -1; 
			}  
		});
		return list;
	}

}
